package com.woniu.liuzhougov.itemQueryAndNotice.controller;

import com.woniu.liuzhougov.authorityControl.constant.UserConstant;
import com.woniu.liuzhougov.authorityControl.pojo.User;
import com.woniu.liuzhougov.itemAndTask.pojo.Notice;
import com.woniu.liuzhougov.itemAndTask.pojo.UserNotice;
import com.woniu.liuzhougov.itemQueryAndNotice.service.NoticeService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
 * @version 1.0
 * @Author yangzhuolin
 * @ClassName NoticeController
 * @Description 处理与公告相关的请求
 * @Date 2019/4/23 14:46
 */
@Controller
@RequestMapping("notice")
public class NoticeController {

    @Autowired
    private NoticeService noticeService;

    @RequestMapping("/addNotice")
    @ResponseBody
    public String addNotice(Notice notice){
        Subject subject = SecurityUtils.getSubject();
        Session session = subject.getSession();
        User user =(User) session.getAttribute(UserConstant.CURRENT_USER);
        notice.setUser(user);
        int addNotice = noticeService.addNotice(notice,user);
        if (addNotice>0){
            return "添加成功！";
        }else {
            return "添加失败！";
        }
    }

    @RequestMapping("/findAll")
    @ResponseBody
    public Map findAllNotice(){
        List<Notice> allNotice = noticeService.findAllNotice();
        //System.out.println(allNotice);
        Map<String,Object> noticeMap = new HashMap<>();
        noticeMap.put("code",0);
        noticeMap.put("msg","");
        noticeMap.put("count",allNotice.size());
        noticeMap.put("data",allNotice);
        return noticeMap;
    }

    /*
       发布个人公告，模拟当前登陆用户currentUserId；
     */
    @RequestMapping("/addPersonalNotice")
    @ResponseBody
    public  String addPersonalNotice(Integer userId, UserNotice userNotice) {
          Subject subject = SecurityUtils.getSubject();
          User currentUser = (User) subject.getSession().getAttribute(UserConstant.CURRENT_USER);
          int personalNotice = noticeService.addPersonalNotice(userNotice, userId, currentUser.getUserId());
            if (personalNotice>0){
                return "添加成功!";
            }else {
               return "添加失败!";
            }
    }

    /*
       查询个人公告，只能本人能够看到；
     */
    @RequestMapping("/findPersonalNotice")
    @ResponseBody
    public Map findPersonalNotice(){
        Subject subject = SecurityUtils.getSubject();
        User current = (User) subject.getSession().getAttribute(UserConstant.CURRENT_USER);
        List<UserNotice> personalNotice = noticeService.findPersonalNotice(current.getUserId());
        //System.out.println(personalNotice);
        Map<String,Object> map = new HashMap<>();
        map.put("code",0);
        map.put("msg","");
        map.put("count",personalNotice.size());
        map.put("data",personalNotice);
        return map;
    }

}
